import { users, bindings, patientDetails } from './mockData';
// 模拟延迟
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// 获取绑定的用户列表
export const mockGetUserList = async (userId) => {
  await delay(500); // 模拟网络延迟
  const boundPatientIds = bindings[userId] || [];
  const boundPatients = boundPatientIds.map(id => {
    const user = users.find(u => u.id === id);
    return user ? { 
      id: user.id,
      name: user.name,
      sex: user.sex,
      age: user.age,
      riskLevel:user.riskLevel
    } : null;
  }).filter(Boolean);
  
  return {
    code: 200,
    list: boundPatients,
    message: 'success'
  };
};

// 绑定病人
export const mockBindingUser = async (userId, bindedUserId) => {
  await delay(500);
  if (!bindings[userId]) {
    bindings[userId] = [];
  }
  
  if (bindings[userId].includes(bindedUserId)) {
    return {
      code: 400,
      message: '该病人已经绑定'
    };
  }
  
  bindings[userId].push(bindedUserId);
  return {
    code: 200,
    message: '绑定成功'
  };
};

// 解绑病人
export const mockUnbindingUser = async (userId, bindedUserId) => {
  await delay(500);
  if (!bindings[userId] || !bindings[userId].includes(bindedUserId)) {
    return {
      code: 400,
      message: '未找到该绑定关系'
    };
  }
  
  bindings[userId] = bindings[userId].filter(id => id !== bindedUserId);
  return {
    code: 200,
    message: '解绑成功'
  };
};

// 获取患者详情
export const mockGetPatientDetail = async (id) => {
  await delay(500);
  const patient = patientDetails[id];
  
  if (!patient) {
    return {
      code: 404,
      message: '未找到该患者'
    };
  }
  
  return {
    code: 200,
    data: patient,
    message: 'success'
  };
}; 

// 获取患者病情
export const mockGetPatientDiagnosis = async (id) => {
	await delay(500)
	const patient = patientDiagnosis[id];
	if (!patient) {
	  return {
	    code: 404,
	    message: '未找到该患者'
	  };
	}
	
	return {
	  code: 200,
	  data: patient,
	  message: 'success'
	};
}